package com.hjwoo.app.views;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.OrdersObject;
import com.hjwoo.app.common.AppManager;
import com.hjwoo.app.ui.CenterAlertDialog;
import com.hjwoo.app.ui.CenterAlertDialogNoTitle;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;

import java.io.ByteArrayOutputStream;

/**
 * Created by yangchuan on 2016/5/10.
 * 报单成功界面
 */
public class DeclarationSuccessActivity extends BaseAppCompatActivity {
    //是否已经签名
    private boolean isSign = false;
    private OrdersObject ordersObject;
    private WebView zx_webview;
    private LinearLayout zxdialog_ll;
    private TextView signature_btn;
    public static Bitmap signbitmap;
    private CenterAlertDialogNoTitle centerAlertDialogNoTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_declarationsuccess);
        //创建Dialog
        centerAlertDialogNoTitle = new CenterAlertDialogNoTitle(
                DeclarationSuccessActivity.this, R.style.Theme_Popdialog, "", "您确认返回首页么?");
        if (getIntent() != null)
        {
            ordersObject = (OrdersObject) getIntent().getSerializableExtra("order");
        }
        zxdialog_ll = (LinearLayout) findViewById(R.id.zxdialog_ll);
        zx_webview = (WebView) findViewById(R.id.zx_webview);
        signature_btn = (TextView) findViewById(R.id.signature_btn);
        zx_webview.loadUrl("file:///android_asset/zxweb/show.html");
        findViewById(R.id.declarationsuccess_orderdetial_btn).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //跳转到订单详情
              Intent mIntent = new Intent(DeclarationSuccessActivity.this, OrderTrackingActivity.class);
              //跳转到预约下户界面
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("order", ordersObject);
                mIntent.putExtras(mBundle);
                startActivity(mIntent);
            }
        });
        findViewById(R.id.commit_signature_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (DeclarationSuccessActivity.signbitmap != null){

                ByteArrayOutputStream stream = new ByteArrayOutputStream();

                DeclarationSuccessActivity.signbitmap.compress(Bitmap.CompressFormat.JPEG, 75, stream);
                }else{
                    LoanToast.showToastShort("请签名后提交",DeclarationSuccessActivity.this);
                }
            }
        });
        //返回弹出Dialog
       findViewById(R.id.dec_suc_btnBack).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                 centerAlertDialogNoTitle.show();
            }
        });

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                AppManager.getAppManager().finishAllActivity();
            }
        });
        signature_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DeclarationSuccessActivity.this,WritePadActivity.class);
                startActivityForResult(intent,0);
            }
        });
    }

    /**
     * 缩放bitmap
     * @param bitmap
     * @return
     */
    private Bitmap small(Bitmap bitmap)
    {
        Matrix matrix = new Matrix();
        matrix.postScale(0.2f, 0.2f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizeBmp;
    }

    /**
     * 将图片变成base64
     * @param bitmap
     * @return
     */
    private String bitmapToString(Bitmap bitmap)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) { //resultCode为回传的标记，我在B中回传的是RESULT_OK
            case RESULT_OK:
                if (DeclarationSuccessActivity.signbitmap!=null){
                    Drawable drawable =new BitmapDrawable(DeclarationSuccessActivity.signbitmap);
                    signature_btn.setBackground(drawable);
                    signature_btn.setText("");
                }else{
                    LoanLog.e("tag","a");
                }
                break;
            default:
                break;
        }
    }
    public void onBackPressed() {
        if (zxdialog_ll.getVisibility()==View.VISIBLE){
            zxdialog_ll.setVisibility(View.GONE);
        }else {
            AppManager.getAppManager().finishAllActivity();

        }
    }
//监听手机返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ){
            centerAlertDialogNoTitle.show();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        centerAlertDialogNoTitle.dismiss();
    }
}
